package com.zyf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zyf.client.DictFeignClient;
import com.zyf.enums.DictEnum;
import com.zyf.model.hosp.Hospital;
import com.zyf.repository.HospitalRepository;
import com.zyf.service.HospitalService;
import com.zyf.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 医院业务  业务层实现类
 *
 * @author zyf
 * @date 2021年05月07日 13:39
 */
@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;

    /**
     * 上传医院信息Api
     *
     * @param paramMap 医院信息的Map集合
     * @author zyf
     * @date 2021/5/7 13:57
     */
    @Override
    public void saveHosp(Map<String, Object> paramMap) {
        String map = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(map, Hospital.class);

        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hospital.getHoscode());

        if (hospitalExist != null) { //如果存在，进行修改
            hospital.setStatus(hospitalExist.getStatus())
                    .setCreateTime(hospitalExist.getCreateTime())
                    .setUpdateTime(new Date())
                    .setIsDeleted(0);
            hospitalRepository.save(hospital);
        } else { //如果不存在，进行新增
            hospital.setStatus(0)
                    .setCreateTime(new Date())
                    .setUpdateTime(new Date())
                    .setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }

    /**
     * 根据医院编号查询医院Api
     *
     * @param hoscode 医院编号
     * @return Hospital
     * @author zyf
     * @date 2021/5/7 17:47
     */
    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital byHoscode = hospitalRepository.getHospitalByHoscode(hoscode);
        return byHoscode;
    }

    /**
     * 分页条件查询医院信息Api
     * @author zyf
     * @date 2021/5/8 18:00
     * @param page 当前页码
     * @param limit 页面记录数
     * @param hospitalQueryVo 条件对象
     * @return org.springframework.data.domain.Page
     */
    @Override
    public Page<Hospital> selectHospByPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //1、创建分页对象  pageable
        Pageable pageable = PageRequest.of(page-1,limit);
        //2、创建条件查询匹配器
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        //3、HospitalQueryVo 转换成Hospital对象
        Hospital hospital = new Hospital();
        if (hospitalQueryVo != null) {
            BeanUtils.copyProperties(hospitalQueryVo,hospital);
        }

        //4、创建对象
        Example<Hospital> example = Example.of(hospital,matcher);
        //5、调用方法实现查询
        Page<Hospital> all = hospitalRepository.findAll(example, pageable);

        //获取查询list集合，遍历进行医院信息封装
        all.getContent().forEach(this::setHospitalHosType);

        return all;
    }

    /**
     * 更新医院上下线状态API
     * @author zyf
     * @date 2021/5/13 15:03
     * @param id 医院id
     * @param status 医院状态
     * @return boolean
     */
    @Override
    public boolean updateHospStatus(String id, Integer status) {
        Optional<Hospital> byId = hospitalRepository.findById(id);
        boolean present = byId.isPresent();
        if (present){
            Hospital hospital = byId.get();
            hospital.setStatus(status)
                    .setUpdateTime(new Date());
            hospitalRepository.save(hospital);
            return true;
        }else {
            return false;
        }
    }

    private Hospital setHospitalHosType(Hospital hospital) {
        //根据dictCode和Value获取医院等级信息
        String hostypeString = dictFeignClient.getName(DictEnum.HOSTYPE.getDictCode(), hospital.getHostype());

        //查询医院的地址信息  省 市 区
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());//省
        String cityString = dictFeignClient.getName(hospital.getCityCode());//市
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());//区

        //封装医院信息
        hospital.getParam().put("fullAddress",provinceString+cityString+districtString); //医院地址信息
        hospital.getParam().put("hostypeString",hostypeString); //医院等级
        return hospital;
    }

    @Override
    public Map<String,Object> findHospById(String id) {
        Map<String,Object> map = new HashMap<>();
        Optional<Hospital> byId = hospitalRepository.findById(id);

        boolean present = byId.isPresent();
        if (present){
            Hospital hospital = byId.get();
            Hospital hospital1 = this.setHospitalHosType(hospital);

            map.put("hospital",hospital1);
            map.put("bookingRule",hospital1.getBookingRule());
            hospital.setBookingRule(null);
        }

        return map;
    }

    /**
     * 根据医院编号获取医院名称
     * @author zyf
     * @date 2021/5/14 10:06
     * @param hoscode 医院编号
     * @return java.lang.String
     */
    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospital != null) {
            return hospital.getHosname();
        }
        return null;
    }

    /**
     * 根据医院名称模糊查询医院信息
     * @author zyf
     * @date 2021/5/17 11:56
     * @param hosname 医院名称
     * @return java.util.List<com.zyf.model.hosp.Hospital>
     */
    @Override
    public List<Hospital> findHospByHosName(String hosname) {
        List<Hospital> list = hospitalRepository.findHospitalByHosnameLike(hosname);
        return list;
    }

    /**
     * 根据医院编号获取医院预约挂号详情
     * @author zyf
     * @date 2021/5/18 9:52
     * @param hoscode 医院编号
     * @return java.util.Map<java.lang.String,java.lang.Object>
     */
    @Override
    public Map<String, Object> item(String hoscode) {
        HashMap<String, Object> map = new HashMap<>();
        //医院详情
        Hospital hospital = this.setHospitalHosType(this.getByHoscode(hoscode));
        map.put("hospital",hospital);

        //挂号规则
        map.put("bookingRule",hospital.getBookingRule());

        //不需要重复返回
        hospital.setBookingRule(null);
        return map;
    }
}
